package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.dao.ChongmingDoctorWorkBloodPressureMapper;
import com.csun.cmny.provider.dao.ChongmingDoctorWorkMedicalAdviceMapper;
import com.csun.cmny.provider.dao.ChongmingDoctorWorkMedicalRecordMapper;
import com.csun.cmny.provider.dao.ChongmingDoctorWorkTemperatureMapper;
import com.csun.cmny.provider.dto.*;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.dto.doctor.HuaLongBloodPressureDownDto;
import com.csun.cmny.provider.model.dto.doctor.HuaLongTemperatureDownDto;
import com.csun.cmny.provider.model.vo.doctor.HuaLongBloodPressureRecordVo;
import com.csun.cmny.provider.model.vo.doctor.HuaLongTemperatureRecordVo;
import com.csun.cmny.provider.pojo.ChongmingDoctorWorkBloodPressure;
import com.csun.cmny.provider.pojo.ChongmingDoctorWorkMedicalAdvice;
import com.csun.cmny.provider.pojo.ChongmingDoctorWorkMedicalRecord;
import com.csun.cmny.provider.pojo.ChongmingDoctorWorkTemperature;
import com.csun.cmny.provider.service.ChongmingDoctorWorkService;
import com.csun.cmny.provider.vo.*;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;


/**
 * @Auther: ZNR
 * @Date: 2020-10-20
 * @Description:
 */
@Service("chongmingDoctorWorkService")
public class ChongmingDoctorWorkServiceImpl implements ChongmingDoctorWorkService {

    @Resource
    private ChongmingDoctorWorkBloodPressureMapper chongmingDoctorWorkBloodPressureMapper;
    @Resource
    private ChongmingDoctorWorkTemperatureMapper chongmingDoctorWorkTemperatureMapper;
    @Resource
    private ChongmingDoctorWorkMedicalAdviceMapper chongmingDoctorWorkMedicalAdviceMapper;
    @Resource
    private ChongmingDoctorWorkMedicalRecordMapper chongmingDoctorWorkMedicalRecordMapper;

    //查询血压记录
    @Override
    public PageInfo selectBloodPressure(DoctorWorkBloodPressureDto doctorWorkBloodPressureDto){

        PageHelper.startPage(doctorWorkBloodPressureDto.getPageNum(), doctorWorkBloodPressureDto.getPageSize());
        List<DoctorWorkBloodPressureVo> doctorWorkBloodPressureVoList = chongmingDoctorWorkBloodPressureMapper.selectBloodPressureListWithPage(doctorWorkBloodPressureDto);

        /**
        for(DoctorWorkBloodPressureVo vo:doctorWorkBloodPressureVoList){
            OmcChongmingDwrMedicalrecord omcChongmingDwrMedicalrecord = omcChongmingDwrMedicalrecordMapper.selectbyOldmanId(vo.getOldmanId());
            BeanUtil.copyProperties(omcChongmingDwrMedicalrecord,vo);
        }
         **/
        return new PageInfo<>(doctorWorkBloodPressureVoList);
    }

    //上传血压记录
    @Override
    public Integer upBloodPressure(DoctorWorkUpBloodPressureDto doctorWorkUpBloodPressureDto){
        ChongmingDoctorWorkBloodPressure bp = new ChongmingDoctorWorkBloodPressure();

        ChongmingDoctorWorkMedicalRecord mr = new ChongmingDoctorWorkMedicalRecord();
        mr.setOldmanId(doctorWorkUpBloodPressureDto.getOldmanId());
        List<ChongmingDoctorWorkMedicalRecord> select = chongmingDoctorWorkMedicalRecordMapper.select(mr);
        if(PublicUtil.isEmpty(select)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10091002);
        }


        ChongmingDoctorWorkMedicalRecord chongmingDoctorWorkMedicalRecord = select.get(0);
        doctorWorkUpBloodPressureDto.setMedicalRecordNo(chongmingDoctorWorkMedicalRecord.getMedicalRecordNo());
        BeanUtils.copyProperties(doctorWorkUpBloodPressureDto, bp);

        // ChongmingDoctorWorkBloodPressure chongmingDoctorWorkBloodPressure = new ChongmingDoctorWorkBloodPressure();
        // BeanUtils.copyProperties(doctorWorkUpBloodPressureDto, chongmingDoctorWorkBloodPressure);

      /*  try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chongmingDoctorWorkBloodPressure.setDetectionTime(format.parse(doctorWorkUpBloodPressureDto.getDetectionTime()));
        }catch (ParseException e){
            e.printStackTrace();
        } */
        return chongmingDoctorWorkBloodPressureMapper.insertSelective(bp);
    }

    //查询体温记录
    @Override
    public PageInfo selectTemperature(DoctorWorkTemperatureDto doctorWorkTemperatureDto) {

        PageHelper.startPage(doctorWorkTemperatureDto.getPageNum(), doctorWorkTemperatureDto.getPageSize());
        List<DoctorWorkTemperatureVo> doctorWorkTemperatureVoList = chongmingDoctorWorkTemperatureMapper.selectTemperatureListWithPage(doctorWorkTemperatureDto);

        return new PageInfo<>(doctorWorkTemperatureVoList);
    }

    //上传体温记录
    @Override
    public Integer upTemperature(DoctorWorkUpTemperatureDto doctorWorkUpTemperatureDto){

        ChongmingDoctorWorkTemperature chongmingDoctorWorkTemperature = new ChongmingDoctorWorkTemperature();
        BeanUtils.copyProperties(doctorWorkUpTemperatureDto,chongmingDoctorWorkTemperature);
      /*  try {
            SimpleDateFormat formatT =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chongmingDoctorWorkTemperature.setDetectionTime(formatT.parse(doctorWorkUpTemperatureDto.getDetectionTime()));
        }catch (ParseException e){
            e.printStackTrace();
        } */
        return chongmingDoctorWorkTemperatureMapper.insertSelective(chongmingDoctorWorkTemperature);
    }

    //查询长期医嘱单
    @Override
    public PageInfo selectMedicalAdvice(DoctorWorkMedicalAdviceDto doctorWorkMedicalAdviceDto){

        PageHelper.startPage(doctorWorkMedicalAdviceDto.getPageNum(),doctorWorkMedicalAdviceDto.getPageSize());
        List<DoctorWorkMedicalAdviceVo> doctorWorkMedicalAdviceVoList = chongmingDoctorWorkMedicalAdviceMapper.selectMedicalAdviceListWithPage(doctorWorkMedicalAdviceDto);
        return new PageInfo<>(doctorWorkMedicalAdviceVoList);
    }

    //添加长期医嘱单
    @Override
    public Integer addMedicalAdvice(DoctorWorkAddMedicalAdviceDto doctorWorkAddMedicalAdviceDto){
        ChongmingDoctorWorkMedicalAdvice chongmingDoctorWorkMedicalAdvice = new ChongmingDoctorWorkMedicalAdvice();
        BeanUtils.copyProperties(doctorWorkAddMedicalAdviceDto,chongmingDoctorWorkMedicalAdvice);
       // try {
            //SimpleDateFormat formatM = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chongmingDoctorWorkMedicalAdvice.setMedicalAdviceTime(doctorWorkAddMedicalAdviceDto.getMedicalAdviceTime());
            chongmingDoctorWorkMedicalAdvice.setStopTime(doctorWorkAddMedicalAdviceDto.getStopTime());
       // }catch (ParseException e){
         //   e.printStackTrace();
      //  }
            chongmingDoctorWorkMedicalAdvice.setStatus(0);
        return chongmingDoctorWorkMedicalAdviceMapper.insertSelective(chongmingDoctorWorkMedicalAdvice);
    }

    //修改长期医嘱
    @Override
    public Integer modifyMedicalAdvice(DoctorWorkModifyMedicalAdviceDto doctorWorkModifyMedicalAdviceDto){
        ChongmingDoctorWorkMedicalAdvice advice = new ChongmingDoctorWorkMedicalAdvice();
        advice.setMedicalAdviceId(doctorWorkModifyMedicalAdviceDto.getMedicalAdviceId());
        List<ChongmingDoctorWorkMedicalAdvice> select = chongmingDoctorWorkMedicalAdviceMapper.select(advice);
        ChongmingDoctorWorkMedicalAdvice chongmingDoctorWorkMedicalAdvice = select.get(0);
        if (chongmingDoctorWorkMedicalAdvice.getStatus() == 1){
            throw new OmcBizException(ErrorCodeEnum.OMC10091003);
        }else {
            Date time1 = chongmingDoctorWorkMedicalAdvice.getMedicalAdviceTime();
            Date time2 = chongmingDoctorWorkMedicalAdvice.getStopTime();
            Date date = new Date();
            if (date.compareTo(time1) < 1 || time2.compareTo(date) < 1) {
                throw new OmcBizException(ErrorCodeEnum.OMC10091001);
//        int comp1 = time1.compareTo(new Date())
            }
//        int comp2 = new Date().compareTo(time2);
//        if (comp1 < 1  && comp2 < 1) {
//            int m = chongmingDoctorWorkMedicalAdviceMapper.modifyMedicalAdvice(doctorWorkModifyMedicalAdviceDto);
//            return m;
//        }
//
            return chongmingDoctorWorkMedicalAdviceMapper.modifyMedicalAdvice(doctorWorkModifyMedicalAdviceDto);
        }
    }

    //结束长期医嘱
    @Override
    public Integer stopMedicalAdvice(DoctorWorkStopMedicalAdviceDto doctorWorkStopMedicalAdviceDto){
        doctorWorkStopMedicalAdviceDto.setStatus(1);
        int n = chongmingDoctorWorkMedicalAdviceMapper.stopMedicalAdvice(doctorWorkStopMedicalAdviceDto);
        return n;
    }

    //删除长期医嘱
    @Override
    public Integer deleteMedicalAdvice(DoctorWorkModifyMedicalAdviceDto doctorWorkModifyMedicalAdviceDto){
        int d = chongmingDoctorWorkMedicalAdviceMapper.deleteMedicalAdvice(doctorWorkModifyMedicalAdviceDto.getMedicalAdviceId());
        return d;
    }

    //查询病程记录
    @Override
    public PageInfo selectMedicalRecord(DoctorWorkMedicalRecordDto doctorWorkMedicalRecordDto){

        PageHelper.startPage(doctorWorkMedicalRecordDto.getPageNum(),doctorWorkMedicalRecordDto.getPageSize());
        List<DoctorWorkMedicalRecordVo> doctorWorkMedicalRecordVoList = chongmingDoctorWorkMedicalRecordMapper.selectMedicalRecordListWithPage(doctorWorkMedicalRecordDto);
        return new PageInfo<>(doctorWorkMedicalRecordVoList);
    }

    @Override
    public List<DoctorWorkMedicalRecordVo> selectMedicalRecordToList(DoctorWorkMedicalRecordDto doctorWorkMedicalRecordDto) {
        return chongmingDoctorWorkMedicalRecordMapper.selectMedicalRecordListWithPage(doctorWorkMedicalRecordDto);
    }

    //添加病程记录
    @Override
    public Integer addMedicalRecord(DoctorWorkAddMedicalRecordDto doctorWorkAddMedicalRecordDto){
        ChongmingDoctorWorkMedicalRecord record = new ChongmingDoctorWorkMedicalRecord();
        record.setOldmanId(doctorWorkAddMedicalRecordDto.getOldmanId());
        List<ChongmingDoctorWorkMedicalRecord> select = chongmingDoctorWorkMedicalRecordMapper.select(record);

        if(PublicUtil.isNotEmpty(select)) {
            ChongmingDoctorWorkMedicalRecord chongmingDoctorWorkMedicalRecord = select.get(0);
            doctorWorkAddMedicalRecordDto.setMedicalRecordNo(chongmingDoctorWorkMedicalRecord.getMedicalRecordNo());
            BeanUtils.copyProperties(doctorWorkAddMedicalRecordDto,record);
            return chongmingDoctorWorkMedicalRecordMapper.insertSelective(record);
        }
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            String dateName = df.format(calendar.getTime());
            doctorWorkAddMedicalRecordDto.setMedicalRecordNo(dateName+doctorWorkAddMedicalRecordDto.getOldmanId().toString());
            BeanUtils.copyProperties(doctorWorkAddMedicalRecordDto,record);
            return chongmingDoctorWorkMedicalRecordMapper.insertSelective(record);


        /*Long a = chongmingDoctorWorkMedicalRecordMapper.selectMedicalRecordNoByOldmanId(doctorWorkAddMedicalRecordDto.getOldmanId());
        if(a == null){
            //throw new OmcBizException(ErrorCodeEnum.)
        }
        ChongmingDoctorWorkMedicalRecord chongmingDoctorWorkMedicalRecord = new ChongmingDoctorWorkMedicalRecord();
        BeanUtils.copyProperties(doctorWorkAddMedicalRecordDto,chongmingDoctorWorkMedicalRecord);
        try {
            SimpleDateFormat formatM = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chongmingDoctorWorkMedicalRecord.setMedicalTime(formatM.parse(doctorWorkAddMedicalRecordDto.getMedicalTime()));
        }catch (ParseException e){
            e.printStackTrace();
        }
        return chongmingDoctorWorkMedicalRecordMapper.insertSelective(chongmingDoctorWorkMedicalRecord);*/
    }

    //修改病程记录
    @Override
    public Integer modifyMedicalRecord(DoctorWorkModifyMedicalRecordDto doctorWorkModifyMedicalRecordDto){
        int mr = chongmingDoctorWorkMedicalRecordMapper.modifyMedicalRecord(doctorWorkModifyMedicalRecordDto);
        return mr;
    }

    //删除病程记录
    @Override
    public Integer deleteMedicalRecord(DoctorWorkModifyMedicalRecordDto doctorWorkModifyMedicalRecordDto){
        int d = chongmingDoctorWorkMedicalRecordMapper.deleteMedicalRecord(doctorWorkModifyMedicalRecordDto.getMedicalRecordId());
        return d;
    }

    /**
     * 体温报表导出（老人Id, 时间）
     *
     * @param huaLongTemperatureDownDto the hua long temperature down dto
     *
     * @return the list.
     */
    @Override
    public List<HuaLongTemperatureRecordVo> queryTemperatureRecord(HuaLongTemperatureDownDto huaLongTemperatureDownDto) {

        return chongmingDoctorWorkTemperatureMapper.queryTemperatureRecord(huaLongTemperatureDownDto);
    }

    /**
     * 血压报表导出（老人Id, 时间）
     *
     * @param huaLongBloodPressureDownDto the hua long blood pressure down dto
     *
     * @return the list.
     */
    @Override
    public List<HuaLongBloodPressureRecordVo> queryBloodPressureRecord(HuaLongBloodPressureDownDto huaLongBloodPressureDownDto) {

        return chongmingDoctorWorkBloodPressureMapper.queryBloodPressureRecord(huaLongBloodPressureDownDto);
    }

    @Override
    public List<DoctorWorkMedicalAdviceDownVo> queryMedicalAdvice(DoctorWorkMedicalAdviceDownDto doctorWorkMedicalAdviceDownDto){
        return chongmingDoctorWorkMedicalAdviceMapper.queryMedicalAdvice(doctorWorkMedicalAdviceDownDto);
    }

}
